package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderSettingService;
import com.xrit.sunnyday.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 类名称: OrderSettingController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/21 上午 10:27
 * Version 1.0.0
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /*文件上传*/
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> excelStringList = POIUtils.readExcel(excelFile);
            ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);
            Date orderDate = null;
            OrderSetting os = null;
            for (String[] dataArr : excelStringList) {
                orderDate = sdf.parse(dataArr[0]);
                int number = Integer.parseInt(dataArr[1]);
                os = new OrderSetting(orderDate,number);
                orderSettingList.add(os);
            }
            orderSettingService.add(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException | NumberFormatException | ParseException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @GetMapping("/getDataByMonth")
    public Result getDataByMonth (String month){
      List<Map<String,Integer>> list=  orderSettingService.getDataByMonth(month);
      return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        System.out.println(orderSetting);
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCECC);
    }
}
